<?php

if (!empty($_POST['msg'])) {

	require 'vars.php';
	require 'config.mail.php';
	require 'functs.php';
	
	Authenticate();

	require 'config.mysql.php';

	$link = MySQL_go($mysql_server,$mysql_user,$mysql_pass);
	MySQL_set_db($link,$mysql_db);

	header("Location: index.php#bot");

	$message = mysql_real_escape_string($_POST[msg]);
	$user = mysql_real_escape_string($_SESSION[user]);

	$timestamp = time();

	$highidsql = "SELECT `msgid` FROM `featherchat_messages` ORDER BY `msgid` DESC LIMIT 1";
	$result = mysql_query($highidsql) or die ("Error retrieving post count.");
	$highid = mysql_result($result,0);
	$msgid = $highid+1;

	$sendmessagesql = "INSERT INTO `featherchat_messages` (`message`, `user`, `timestamp`, `msgid`) VALUES ('".$message."', '".$user."', '".$timestamp."', '".$msgid."')";
	if(mysql_query($sendmessagesql) or die ("Unable to insert post into database.")) { echo "Message sent!<br /><br />"; }

	if ($email_enable == "1") { // Don't try to send an e-mail if the admin hasn't configured it.

		$emailsql = "SELECT `name`,`email`,`emailnot`,`emailtime`,`lastseen`,`notified` FROM `featherchat_users` WHERE `name` != '".$user."'";
		$emailquery = mysql_query($emailsql);

			while ($email_r = mysql_fetch_assoc($emailquery)) {

			$emailtime = $email_r[emailtime]*60;
			if ($email_r[emailnot] == 1 && (time()-$emailtime > $email_r[lastseen]) && ($email_r[lastseen] > $email_r[notified])) {
		
				$name = mysql_real_escape_string($email_r[name]);
			
				$notifysql = "UPDATE `featherchat_users` SET `notified` = '".time()."' WHERE `name` = '".$name."' LIMIT 1";
				mysql_query($notifysql) or die ("Error modifying e-mail notification in database.");
				Email_notify($email_r,$email_config,$_SESSION[user]);
			}
		}
	}
	?><p><a href="index.php#bot">Home</a></p><?php
	mysql_close($link);
} else { header("Location: index.php#bot"); } // An empty post results in a refresh.
?>


